package cn.site.modules.sys.entity;

import cn.site.core.utils.IdGen;
import cn.site.core.utils.StringUtils;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

@Entity
@Table(name = "sys_log")
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Log implements Serializable {

    private static final long serialVersionUID = 1L;

    // 日志类型（1：接入日志；2：错误日志）
    public static final String TYPE_ACCESS = "1";
    public static final String TYPE_EXCEPTION = "2";

    @Id
    private String id;
    @Column(name = "type", length = 1)
    private String type;            // 日志类型（1：接入日志；2：错误日志）
    @Column(name = "title", length = 255)
    private String title;           // 日志标题
    @Column(name = "remote_addr", length = 30)
    private String remoteAddr;      // 操作用户的IP地址
    @Column(name = "request_uri", length = 255)
    private String requestUri;      // 操作的URI
    @Column(name = "method", length = 100)
    private String method;          // 操作的方式
    @Column(name = "params", length = 255)
    private String params;          // 操作提交的数据
    @Column(name = "user_agent", length = 100)
    private String userAgent;       // 操作用户代理信息
    @Column(name = "exception", length = 255)
    private String exception;       // 异常信息
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="begin_date")
    private Date beginDate;         // 开始日期
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="end_date")
    private Date endDate;           // 结束日期

    @PrePersist
    public void prePersist(){
        this.id = IdGen.uuid();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRemoteAddr() {
        return remoteAddr;
    }

    public void setRemoteAddr(String remoteAddr) {
        this.remoteAddr = remoteAddr;
    }

    public String getRequestUri() {
        return requestUri;
    }

    public void setRequestUri(String requestUri) {
        this.requestUri = requestUri;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public Date getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * 设置请求参数
     *
     * @param paramMap
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void setParams(Map paramMap) {
        if (paramMap == null) {
            return;
        }
        StringBuilder params = new StringBuilder();
        for (Map.Entry<String, String[]> param : ((Map<String, String[]>) paramMap).entrySet()) {
            params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
